package cn.iocoder.an.module.business.job;

import cn.iocoder.an.framework.common.util.object.BeanUtils;
import cn.iocoder.an.framework.quartz.core.handler.JobHandler;
import cn.iocoder.an.framework.tenant.core.job.TenantJob;
import cn.iocoder.an.module.business.controller.admin.storeinit.vo.StoreInitRespVO;
import cn.iocoder.an.module.business.controller.admin.storemanage.vo.StoreManagePageReqVO;
import cn.iocoder.an.module.business.dal.dataobject.storeinit.StoreInitDO;
import cn.iocoder.an.module.business.dal.dataobject.storeinitbackup.StoreInitBackupDO;
import cn.iocoder.an.module.business.dal.mysql.storeinit.StoreInitMapper;
import cn.iocoder.an.module.business.dal.mysql.storeinitbackup.StoreInitBackupMapper;
import cn.iocoder.an.module.business.dal.mysql.storemanage.StoreManageMapper;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@Component

public class BackUpStoreDataJob implements JobHandler {

    @Resource
    private StoreInitMapper storeInitMapper;

    @Resource
    private StoreManageMapper storeManageMapper;


    @Resource
    private StoreInitBackupMapper storeInitBackupMapper;

    @TenantJob
    @Override
    public String execute(String param) throws Exception {

        List<StoreInitBackupDO> backupDOList = new ArrayList<>();

        List<StoreInitDO> storeInitDOS = storeInitMapper.selectList();
        System.out.println(storeInitDOS.size());

        storeInitMapper.selectList().forEach(storeInit -> {
            //期初库存
            Integer initAmount = storeInit.getInitAmount();

            //查询历史出入库
            StoreManagePageReqVO reqVO = new StoreManagePageReqVO();
            reqVO.setCustomerAccount(storeInit.getCustomerAccount());
            reqVO.setDrawingNumber(storeInit.getDrawingNumber());
            Long inCountLong = storeManageMapper.selectInCount(reqVO);
            //总入库
            Integer inCount = (inCountLong == null) ? 0 : inCountLong.intValue();
            //总出库
            Long outCountLong = storeManageMapper.selectOutCount(reqVO);
            Integer outCount = (outCountLong == null) ? 0 : outCountLong.intValue();
            //当期库存
            Integer currentAmount = initAmount + inCount - outCount;
            storeInit.setCurrentAmount(currentAmount);

            //=============================
            //查询历史出入库
            StoreManagePageReqVO currentDayReq = new StoreManagePageReqVO();
            currentDayReq.setCustomerAccount(storeInit.getCustomerAccount());
            currentDayReq.setDrawingNumber(storeInit.getDrawingNumber());
            currentDayReq.setOperDate(LocalDate.now().minusDays(1));
            Long incurrDayLong = storeManageMapper.selectInCount(currentDayReq);
            //总入库 当天
            Integer inCurrentDayAmount = (incurrDayLong == null) ? 0 : incurrDayLong.intValue();
            //总出库 当天
            Long outCurrentDayLong = storeManageMapper.selectOutCount(currentDayReq);
            Integer outCurrentDayCount = (outCurrentDayLong == null) ? 0 : outCurrentDayLong.intValue();
            //=============================

            StoreInitBackupDO backupDO = new StoreInitBackupDO();

            backupDO.setCurrentIn(inCurrentDayAmount);

            backupDO.setCurrentOut(outCurrentDayCount);
            //备份日期 当天的减去一天
            backupDO.setBackupDate(LocalDate.now().minusDays(1));
            backupDO.setCustomerAccount(storeInit.getCustomerAccount());
            backupDO.setCustomerName(storeInit.getCustomerName());
            backupDO.setDrawingNumber(storeInit.getDrawingNumber());
            backupDO.setInitAmount(initAmount);
            backupDO.setStoreLocation(storeInit.getStoreLocation());
            backupDO.setStoreName(storeInit.getStoreName());
            backupDO.setCurrentAmount(currentAmount);
            backupDO.setProductName(storeInit.getProductName());
            backupDOList.add(backupDO);


        });

        if (backupDOList.size() > 0) {
            storeInitBackupMapper.insertBatch(backupDOList);
        }

        return "success";
    }

    //更新一下备份表
    public void updateData(){
        storeInitBackupMapper.selectList().forEach(storeBack -> {
            String customerAccount = storeBack.getCustomerAccount();
            String drawingNumber = storeBack.getDrawingNumber();
            LocalDate backupDate = storeBack.getBackupDate();


            StoreManagePageReqVO currentDayReq = new StoreManagePageReqVO();
            currentDayReq.setCustomerAccount(customerAccount);
            currentDayReq.setDrawingNumber(drawingNumber);
            currentDayReq.setOperDate(backupDate);
            Long incurrDayLong = storeManageMapper.selectInCount(currentDayReq);
            //总入库 当天
            Integer inCurrentDayAmount = (incurrDayLong == null) ? 0 : incurrDayLong.intValue();
            //总出库 当天
            Long outCurrentDayLong = storeManageMapper.selectOutCount(currentDayReq);
            Integer outCurrentDayCount = (outCurrentDayLong == null) ? 0 : outCurrentDayLong.intValue();
            storeBack.setCurrentIn(inCurrentDayAmount);
            storeBack.setCurrentOut(outCurrentDayCount);
            storeInitBackupMapper.updateById(storeBack);
        });
    }


}
